package com.acdc.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.acdc.domain.OtaSplit;
import com.acdc.mapper.OtaVersionFileMapper;
import com.acdc.domain.OtaVersionFile;
import com.acdc.service.IOtaVersionFileService;
import com.acdc.utils.FileBinarySplitter;
import com.acdc.utils.FileBinarySplitter.ChunkWithChecksum;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import com.ruoyi.framework.config.ServerConfig;
/**
 * OTA版本Service业务层处理
 * 
 * @author acdc
 * @date 2025-08-12
 */
@Service
@Slf4j
public class OtaVersionFileServiceImpl implements IOtaVersionFileService 
{
    @Autowired
    private OtaVersionFileMapper otaVersionFileMapper;

    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询OTA版本
     * 
     * @param id OTA版本主键
     * @return OTA版本
     */
    @Override
    public OtaVersionFile selectOtaVersionFileById(Long id)
    {
        return otaVersionFileMapper.selectOtaVersionFileById(id);
    }

    /**
     * 查询OTA版本列表
     * 
     * @param otaVersionFile OTA版本
     * @return OTA版本
     */
    @Override
    public List<OtaVersionFile> selectOtaVersionFileList(OtaVersionFile otaVersionFile)
    {
        return otaVersionFileMapper.selectOtaVersionFileList(otaVersionFile);
    }

    /**
     * 新增OTA版本
     * 
     * @param otaVersionFile OTA版本
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOtaVersionFile(OtaVersionFile otaVersionFile) throws IOException
    {
        String filePath = otaVersionFile.getFilePath().replace("/profile", "./uploadPath");
        List<ChunkWithChecksum> chunks = FileBinarySplitter.splitFileWithChecksum(filePath);
        List<OtaSplit> otaSplitList = new ArrayList<>();
        int seqNum = 1;
        for (ChunkWithChecksum chunk : chunks) {
            OtaSplit otaSplit = new OtaSplit();
            otaSplit.setOtaVersionId(otaVersionFile.getId());
            otaSplit.setSeqNum(seqNum);
            otaSplit.setLength(chunk.getData().length);
            StringBuilder hex = new StringBuilder();
            for (byte b : chunk.getData()) {
                hex.append(String.format("%02X ", b));
            }
            otaSplit.setData(hex.toString().trim());
            otaSplit.setChecksum(chunk.getChecksum());
            otaSplitList.add(otaSplit);
            seqNum++;
        }
        otaVersionFile.setOtaSplitList(otaSplitList);
        int rows = otaVersionFileMapper.insertOtaVersionFile(otaVersionFile);
        insertOtaSplit(otaVersionFile);
        return rows;
    }

    /**
     * 修改OTA版本
     * 
     * @param otaVersionFile OTA版本
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOtaVersionFile(OtaVersionFile otaVersionFile) throws IOException
    {
        String filePath = otaVersionFile.getFilePath().replace("/profile", "./uploadPath");
        List<ChunkWithChecksum> chunks = FileBinarySplitter.splitFileWithChecksum(filePath);
        List<OtaSplit> otaSplitList = new ArrayList<>();
        int seqNum = 1;
        for (ChunkWithChecksum chunk : chunks) {
            OtaSplit otaSplit = new OtaSplit();
            otaSplit.setOtaVersionId(otaVersionFile.getId());
            otaSplit.setSeqNum(seqNum);
            otaSplit.setLength(chunk.getData().length);
            StringBuilder hex = new StringBuilder();
            for (byte b : chunk.getData()) {
                hex.append(String.format("%02X ", b));
            }
            otaSplit.setData(hex.toString().trim());
            otaSplit.setChecksum(chunk.getChecksum());
            otaSplitList.add(otaSplit);
            seqNum++;
        }
        otaVersionFile.setOtaSplitList(otaSplitList);
        otaVersionFileMapper.deleteOtaSplitByOtaVersionId(otaVersionFile.getId());
        insertOtaSplit(otaVersionFile);
        return otaVersionFileMapper.updateOtaVersionFile(otaVersionFile);
    }

    /**
     * 批量删除OTA版本
     * 
     * @param ids 需要删除的OTA版本主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOtaVersionFileByIds(Long[] ids)
    {
        otaVersionFileMapper.deleteOtaSplitByOtaVersionIds(ids);
        return otaVersionFileMapper.deleteOtaVersionFileByIds(ids);
    }

    /**
     * 删除OTA版本信息
     * 
     * @param id OTA版本主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOtaVersionFileById(Long id)
    {
        otaVersionFileMapper.deleteOtaSplitByOtaVersionId(id);
        return otaVersionFileMapper.deleteOtaVersionFileById(id);
    }

    /**
     * 新增OTA分割信息
     * 
     * @param otaVersionFile OTA版本对象
     */
    public void insertOtaSplit(OtaVersionFile otaVersionFile)
    {
        List<OtaSplit> otaSplitList = otaVersionFile.getOtaSplitList();
        Long id = otaVersionFile.getId();
        if (StringUtils.isNotNull(otaSplitList))
        {
            List<OtaSplit> list = new ArrayList<OtaSplit>();
            for (OtaSplit otaSplit : otaSplitList)
            {
                otaSplit.setOtaVersionId(id);
                list.add(otaSplit);
            }
            if (list.size() > 0)
            {
                otaVersionFileMapper.batchOtaSplit(list);
            }
        }
    }

    /**
     * 查找最新OTA版本
     * 
     * @return OTA版本
     */
    @Override
    public OtaVersionFile selectLatestOtaVersionFile()
    {
        return otaVersionFileMapper.selectLatestOtaVersionFile();
    }

    /**
     * 查找OTA分割
     * 
     * @param otaSplit OTA分割
     * @return OTA分割
     */
    @Override
    public OtaSplit selectOtaSplitByOtaVersionIdAndSeqNum(OtaSplit otaSplit)
    {
        return otaVersionFileMapper.selectOtaSplitByOtaVersionIdAndSeqNum(otaSplit);
    }
}
